﻿package org.openChart.display 
{
	import flash.geom.Point;
	
	/**
	 * The sticks on a axis.
	 * 
	 * @author Chongyuan
	 */
	public class DisplayStickGroup 
	{
		// The array of sticks.
		private var sticks_:Array;
		// The max heigth of this group.
		private var maxHeight_:Number = Number.MIN_VALUE;
		// The count of sticks in this group.
		private var length_:uint;
		
		public function DisplayStickGroup() 
		{
			sticks_ = new Array;
		}
		
		/**
		 * Add a stick to group.
		 * @param	displayStick
		 */
		public function addStick(displayStick:DisplayStick):void
		{
			sticks_.push(displayStick);
			if (maxHeight_ < displayStick.height)
				maxHeight_ = displayStick.height;
			length_++;
		}
		
		/**
		 * Remove all sticks from group.
		 */
		public function clear():void
		{
			for (var i:uint = 0; i < length; i++ )
			{
				sticks_.pop();
			}
			length_ = 0;
			maxHeight_ = Number.MIN_VALUE;
		}
		
		/**
		 * Get stick at position index.
		 * @param	index
		 * @return
		 */
		public function getAt(index:uint):DisplayStick
		{
			if (index >= length_ || index < 0)
				throw new Error("index out of boundary");
			var stick:DisplayStick = sticks_[index] as DisplayStick;
			return stick;
		}
		
		/**
		 * set the start and end position of stick.
		 * @param	index
		 * @param	position_1
		 * @param	position_2
		 */
		public function setPosition(index:uint, position_1:Point, position_2:Point):void
		{
			if (index >= length_ || index < 0)
				throw new Error("index out of boundary");
			var stick:DisplayStick = sticks_[index] as DisplayStick;
			stick.startPosition = position_1;
			stick.endPosition = position_2;
		}
		
		// read-only properties.
		public function get maxHeight():Number
		{
			return maxHeight_;
		}
		
		public function get length():Number
		{
			return length_;
		}
		
		public function toString():String		
		{
			var str:String = "\nObject:DisplayStickGroup ";
			str += "[maxHeight = " + maxHeight + "]";
			str += "[length = " + length + "]";
			for (var i:uint = 0; i < length; i++ )
			{
				str += getAt(i);
			}
			return str;
		}
		
	}
	
}